package mshop.com.schild_kotlin.fragments.item_hot

import android.os.Bundle
import android.support.v4.widget.SwipeRefreshLayout
import android.support.v7.widget.LinearLayoutManager
import kotlinx.android.synthetic.main.fragment_home.*
import mshop.com.schild_kotlin.R
import mshop.com.schild_kotlin.adapters.RankAdapter
import mshop.com.schild_kotlin.base.BaseFragment
import mshop.com.schild_kotlin.entitys.HotBean
import mshop.com.schild_kotlin.entitys.ItemList
import mshop.com.schild_kotlin.mvp.base.BaseContract
import mshop.com.schild_kotlin.mvp.presenter.HotPresenter

/**
 * Created by mayi on 2018/5/23.
 * @Autor CaiWF
 * @Email 401885064@qq.com
 * @TODO  最热 -- 排行榜
 */

class RankFragment : BaseFragment(), BaseContract.View<HotBean> {
    var rankAdapter: RankAdapter? = null
    var hotPresenter: HotPresenter? = null
    var mList = ArrayList<ItemList>()
    var strategy: String = "historical"

    //静态方法
    companion object {
        fun getInstall(strategy: String): RankFragment {
            val home_fragment = RankFragment()
            val bundle = Bundle()
            bundle.putString("strategy", strategy)
            home_fragment.arguments = bundle
            return home_fragment
        }
    }

    override fun initView() {
        strategy = arguments?.getString("strategy", "historical") ?: "historical"
        rankAdapter = RankAdapter(mList)
        recycle_view.layoutManager = LinearLayoutManager(context)
        recycle_view.adapter = rankAdapter
        rankAdapter?.notifyDataSetChanged()
        hotPresenter = HotPresenter(strategy, context, this)
        //设置下拉刷新监听
        refreshLayout?.setOnRefreshListener(this)
    }

    override fun getLayoutResources(): Int {
        return R.layout.fragment_home
    }

    override fun setData(bean: HotBean) {
        if (refreshLayout!!.isRefreshing) {
            refreshLayout?.isRefreshing = false
            if (mList.size > 0) {
                mList.clear()
            }
        }

        bean.itemList
                .filter { it.type.equals("video") }
                .forEach { mList.add(it) }
        rankAdapter?.notifyDataSetChanged()
    }

    /**
     * Called when a swipe gesture triggers a refresh.
     */
    override fun onRefresh() {
        hotPresenter?.start()
    }

    override fun getNeedRefresh(): Boolean {
        return true
    }


}